import 'package:flutter/material.dart';
import 'package:helloworld/res/debtData.dart';
import 'package:helloworld/tabs.dart';
import 'package:dio/dio.dart';
import 'package:loading/loading.dart';
import 'package:loading/indicator/ball_pulse_indicator.dart';
import 'dart:ui';
class Debt extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Debt();
  }
}

class _Debt extends State<Debt> with WidgetsBindingObserver{
  Color _color;
  Color _loadColor;
  Color _debtColor;
  Color _textColor;
  Color _boxColor;
  int _index;
  int flag = 1;
  Future<Null> _refresh()async{
    setState(() {
      flag =1;
      debtData.clear();
    });
    await _getData();
  }
  _getData() async{
    Response response = await Dio().get("http://123.207.114.127:1314/debt/selectAll");
    setState(() {
      if(response.data.length==0){
        flag = 0;
      }
      debtData = response.data;
    });


  }
  Widget loadingData(){
    if(debtData.length==0&&flag==1){
      return Container(
          child: Center(
          child: Loading(indicator: BallPulseIndicator(), size: 100.0,color: _loadColor),
    ));
    }else if(debtData.length==0&&flag==0){
      return Container(
          child: Center(
            child: ListView(
              children: <Widget>[
                SizedBox(height: 200,),
                Center(
                  child: Text('暂无负债信息',style: TextStyle(color: _textColor,fontSize: 30),),
                )
              ],
            )
          ));
    }else{
      return ListView(
        physics:  AlwaysScrollableScrollPhysics(),
        children: debtData.asMap().keys.map((f) {
          return Column(
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                    border: Border.all(width: 1,color: _boxColor),
                    borderRadius: BorderRadius.circular(15)
                ),
                child: ListTile(
                  leading: Icon(
                    Icons.monetization_on,
                    size: 40,
                  ),
                  title: Text(debtData[f]['debtname']),
                  subtitle: Text(debtData[f]['debtnote']),
                  trailing: Text(
                    debtData[f]['debt'].toStringAsFixed(2),
                    style: TextStyle(fontSize: 15),
                  ),
                  selected: f == _index,
                  onTap: () {
                    setState(() {
                      _index = f;
                      Navigator.pushNamed(context, '/debtDetail',
                          arguments: {'id': _index});
                    });
                  },
                ),
              ),
              SizedBox(height: 10,)
            ],
          );
        }).toList(),
      );
    }
  }
  Widget loadingTotal(){
    if(debtData.length==0){
      return Text(
        '0.00',
        style: TextStyle(color: _debtColor, fontSize: 40),
      );
    }else{
      return myDebt(debtData,_debtColor);
    }
  }
  @override
  void didChangePlatformBrightness() {
    // TODO: implement didChangePlatformBrightness
    super.didChangePlatformBrightness();
    if(window.platformBrightness==Brightness.dark){
      _color = Colors.black45;
      _loadColor = Colors.white;
      _textColor = Colors.white70;
      _debtColor = Colors.white;
      _boxColor = Colors.grey;
    }else{
      _color = Colors.lightBlueAccent;
      _loadColor = Colors.lightBlue;
      _textColor = Colors.black54;
      _debtColor = Colors.black54;
      _boxColor = Colors.black26;
    }
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    if(window.platformBrightness==Brightness.dark){
      _color = Colors.black45;
      _loadColor = Colors.white;
      _textColor = Colors.white70;
      _debtColor = Colors.white;
      _boxColor = Colors.grey;
    }else{
      _color = Colors.lightBlueAccent;
      _loadColor = Colors.blue;
      _textColor = Colors.black54;
      _debtColor = Colors.black54;
      _boxColor = Colors.black26;
    }
    _getData();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Column(
      children: <Widget>[
        SizedBox(height: 10,),
        Padding(
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
            child: Center(
              child: Column(
                children: <Widget>[
                  Text('负债',style: TextStyle(color: _textColor),),
                  loadingTotal(),
                  Padding(
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          child: Center(
                              child: Container(
                                child: FlatButton(
                                  child:Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Icon(Icons.refresh,color: _textColor,),
                                      SizedBox(width: 10,),
                                      Text(
                                        '暂定刷新',
                                        style:
                                        TextStyle(fontSize: 15, color: _textColor),
                                      ),
                                    ],
                                  ),
                                  onPressed: () {
                                    setState(() {
                                      flag = 1;
                                      debtData.clear();
                                      _getData();
                                    });
                                  },
                                ),
                                height: 55,
                              )
                          ),
                          flex: 1,
                        ),
                        Expanded(
                          child: Container(
                            height: 30,
                            width: 1,
                            decoration: BoxDecoration(
                                border: Border.all(color: _boxColor)),
                          ),
                          flex: 0,
                        ),
                        Expanded(
                          child: Center(
                              child: Container(
                                child: FlatButton(
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Icon(Icons.add,color: _textColor,),
                                      SizedBox(width: 10,),
                                      Text(
                                        '添加负债',
                                        style:
                                        TextStyle(fontSize: 15, color: _textColor),
                                      ),
                                    ],
                                  ),
                                  onPressed: () {
                                    Navigator.pushNamed(context, '/addDebt');
                                  },
                                ),
                                height: 55,
                              )
                          ),
                          flex: 1,
                        ),
                      ],
                    ),
                    padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
                  ),
                ],
              ),
            ),
            width: double.infinity,
            //128 iphone11
            // height: 138,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                color: _color,
                // gradient: LinearGradient(
                //   begin: Alignment.centerLeft,
                //   end: Alignment.centerRight,
                //   colors: [Colors.blueAccent, _color],
                // ),
                boxShadow: [
                  // BoxShadow(
                  //     color: Colors.black12,
                  //     offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                  //     blurRadius: 15.0, //阴影模糊程度
                  //     spreadRadius: 1.0 //阴影扩散程度
                  // )
                ]
            ),
          ),
          padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
        ),
        SizedBox(
          height: 10,
        ),
        Expanded(
          child: Container(
            child: RefreshIndicator(
              displacement: 20,
              child: loadingData(),
              onRefresh: _refresh,
            ),
            padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
          ),
        ),
      ],
    );
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }
}

class myDebt extends StatelessWidget {
  double _depositCount = 0;
  Color _debtColor;
  myDebt(List debtData,Color debtColor) {
    _debtColor = debtColor;
    for (int i = 0; i < debtData.length; i++) {
      _depositCount = _depositCount + debtData[i]['debt'];
    }
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Text(
      _depositCount.toStringAsFixed(2),
      style: TextStyle(color: _debtColor, fontSize: 40),
    );
  }
}
